;-------------------------------------------------------------------------------
format ELF64 executable
entry start

SDL_OPENGL = 0x04000000
SDL_NOFRAME = 0x02000000
SDL_KEYDOWN = 0x300

GL_FRAGMENT_SHADER = 0x8B30

DEBUG = 0

macro emit [param] {param}
;-------------------------------------------------------------------------------
include 'import64.inc'
interpreter '/lib64/ld-linux-x86-64.so.2'
needed 'libdl.so.2'
import dlopen,dlsym
;-------------------------------------------------------------------------------
segment readable writeable executable

start:
    sub rsp,2048
    ; define local variables
    emit <event equ rsp+1024>,<log equ rsp>
    mov r13,[dlsym]
if DEBUG = 1
    ; load libc
    emit <mov rdi,g_str_libc>,<xor esi,esi>,<inc esi>,<call [dlopen]>,<mov [g_libc],rax>
    emit <mov rdi,[g_libc]>,<mov rsi,g_str_printf>,<call r13>,<mov [printf],rax>
end if
    ; load SDL library
    emit <mov rdi,g_str_libsdl>,<xor esi,esi>,<inc esi>,<call [dlopen]>,<mov r12,rax>
    ; SDL_SetVideoMode
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_SetVideoMode>,<call r13>
    emit <mov edi,800>,<mov esi,600>,<xor edx,edx>,<mov ecx,SDL_OPENGL+SDL_NOFRAME>,<call rax>
    ; load needed SDL functions
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_GL_GetProcAddress>,<call r13>,<mov rbx,rax>
    ; create program
    emit <mov rdi,g_str_glCreateShaderProgramv>,<call rbx>
    emit <mov edi,GL_FRAGMENT_SHADER>,<mov esi,1>,<mov rdx,g_src_fs_ptr>,<call rax>,<mov r15d,eax>
    ; use program
    emit <mov rdi,g_str_glUseProgram>,<call rbx>
    emit <mov edi,r15d>,<call rax>
if DEBUG = 1
    ; print GLSL info log
    emit <mov rdi,g_str_glGetProgramInfoLog>,<call rbx>
    emit <mov edi,r15d>,<mov esi,1024>,<xor edx,edx>,<lea rcx,[log]>,<call rax>
    emit <mov rdi,g_str_log>,<lea rsi,[log]>,<call [printf]>
end if
      .main_loop:
    ; process window events
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_PollEvent>,<call r13>
    emit <lea rdi,[event]>,<call rax>
    cmp dword [event],SDL_KEYDOWN
    je .main_loop_end
    ; get time
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_GetTicks>,<call r13>,<call rax>,<mov r14d,eax>
    emit <mov rdi,g_str_glTexCoord1f>,<call rbx>
    emit <cvtsi2ss xmm0,r14d>,<call rax>
    ; draw
    emit <mov rdi,g_str_glRecti>,<call rbx>
    emit <mov edi,-1>,<mov esi,-1>,<mov edx,1>,<mov ecx,1>,<call rax>
    ; swap
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_GL_SwapBuffers>,<call r13>,<call rax>
    jmp .main_loop
      .main_loop_end:
    ; terminate process
    emit <mov eax,60>,syscall
;-------------------------------------------------------------------------------
align 1
g_src_fs:
db  'float t;'
db  'void main(){'
db      't=gl_TexCoord[0].s*.001;'
db      'float x=gl_FragCoord.x;'
db      'float y=gl_FragCoord.y;'
db      'float mov0=x+y+cos(sin(t)*2.)*100.+sin(x/100.)*1000.;'
db      'float mov1=y/600./.2+t;'
db      'float mov2=x/800./.2;'
db      'float c1=abs(sin(mov1+t)/2.+mov2/2.-mov1-mov2+t);'
db      'float c2=abs(sin(c1+sin(mov0/1000.+t)+sin(y/40.+t)+sin((x+y)/100.)*3.));'
db      'float c3=abs(sin(c2+cos(mov1+mov2+c2)+cos(mov2)+sin(x/1000.)));'
db      'gl_FragColor=vec4(c1,c2,c3,1.);'
db  '}',0

g_str_libsdl db 'libSDL-1.3.so.0',0
g_str_SDL_SetVideoMode db 'SDL_SetVideoMode',0
g_str_SDL_PollEvent db 'SDL_PollEvent',0
g_str_SDL_GL_SwapBuffers db 'SDL_GL_SwapBuffers',0
g_str_SDL_GetTicks db 'SDL_GetTicks',0
g_str_SDL_GL_GetProcAddress db 'SDL_GL_GetProcAddress',0
g_str_glCreateShaderProgramv db 'glCreateShaderProgramv',0
g_str_glRecti db 'glRecti',0
g_str_glUseProgram db 'glUseProgram',0
g_str_glTexCoord1f db 'glTexCoord1f',0
if DEBUG = 1
g_str_libc db 'libc.so.6',0
g_str_printf db 'printf',0
g_str_glGetProgramInfoLog db 'glGetProgramInfoLog',0
g_str_log db 'GLSL info log:',10,'%s',10,0
g_str_d db '%d',10,0
g_str_p db '%p',10,0
end if

align 8
g_src_fs_ptr dq g_src_fs

if DEBUG = 1
g_libc dq 0
printf dq 0
end if
;-------------------------------------------------------------------------------
